//
//  WCTConfig.m
//  WeChatTool
//
//  Created by TerryChe on 2016/12/2.
//  Copyright © 2016年 TerryChe. All rights reserved.
//

#import "WCTConfig.h"
#import<CommonCrypto/CommonDigest.h>

@implementation WCTConfig

/**
 *  单例模式，
 *
 *  @return WCTConfig
 */
+(instancetype)sharedInstance{
    static WCTConfig *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
        
        sharedInstance.sleep = 60;//5分钟
        sharedInstance.batchCount = 50;
        sharedInstance.beginDate = NSTimeIntervalSince1970;
        sharedInstance.appStartTime = 0;
        
        sharedInstance.configDic = [NSMutableDictionary dictionaryWithCapacity:50];
        sharedInstance.cacheFriend = [NSMutableDictionary dictionaryWithCapacity:50];
        sharedInstance.cacheChatRoom = [NSMutableArray arrayWithCapacity:50];
        sharedInstance.cacheChatMsg = [NSMutableDictionary dictionaryWithCapacity:50];
        sharedInstance.cacheImg = [NSMutableArray arrayWithCapacity:50];
        sharedInstance.cachePayMsgid = [NSMutableArray arrayWithCapacity:50];
    });
    
    return sharedInstance;
}

-(BOOL)hasConfig{
    @try
    {
        NSString *path = [str_wct_dir stringByAppendingPathComponent:str_wct_config];
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
            return NO;
        }
        
        self.configDic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
        
        if (self.configDic != nil && self.configDic.count > 0) {
            self.address = self.configDic[@"address"];
            self.appID = self.configDic[@"appID"];
            self.appKey = self.configDic[@"appKey"];
            self.nonce = self.configDic[@"nonce"];
            self.token = self.configDic[@"token"];
            
            if (self.configDic[@"expiresIn"] == nil) {
                self.expiresIn = NSTimeIntervalSince1970;
                [self.configDic setObject:@(self.expiresIn) forKey:@"expiresIn"];
            }
            else{
                self.expiresIn = [self.configDic[@"expiresIn"] unsignedLongLongValue];
            }
            
            if (self.configDic[@"beginDate"] == nil) {
                self.beginDate = NSTimeIntervalSince1970;
                [self.configDic setObject:@(self.beginDate) forKey:@"beginDate"];
            }
            else{
                self.beginDate = [self.configDic[@"beginDate"] unsignedLongLongValue];
            }
            
            if (self.configDic[@"appStartTime"] == nil) {
                self.appStartTime = 0;
                [self.configDic setObject:@(self.beginDate) forKey:@"appStartTime"];
            }
            else{
                self.appStartTime = [self.configDic[@"appStartTime"] unsignedLongLongValue];
            }
            
            //写入版本号
            [self.configDic setObject:@(wct_version) forKey:@"version"];
            [self.configDic setObject:wct_version_name forKey:@"versionName"];
            [self.configDic writeToFile:path atomically:YES];
            
            return YES;
        }
        
        return NO;
    }
    @catch (NSException *exception) {
        return NO;
    }
}

-(void)updateToken:(NSString *)token ExpiresIn:(UInt64)expiresIn{
    self.token = token;
    self.expiresIn = expiresIn;
    
    [self.configDic setObject:token forKey:@"token"];
    [self.configDic setObject:@(expiresIn) forKey:@"expiresIn"];
    NSString *configPath = [str_wct_dir stringByAppendingPathComponent:str_wct_config];
    [self.configDic writeToFile:configPath atomically:YES];
}

-(void)updateAppStartTime {
    self.appStartTime = [[NSDate date] timeIntervalSince1970];
    
    [self.configDic setObject:@(self.appStartTime) forKey:@"appStartTime"];
    NSString *configPath = [str_wct_dir stringByAppendingPathComponent:str_wct_config];
    [self.configDic writeToFile:configPath atomically:YES];
}

//sha1加密方式
- (NSString *) sha1:(NSString *)input {
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
    return [self sha1WithData:data];
}

- (NSString *) sha1WithData:(NSData *)input {
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(input.bytes, (unsigned int)input.length, digest);
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    
    return output;
}

//md5加密方式
- (NSString *) md5:(NSString *) input {
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
    uint8_t digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5(data.bytes, (unsigned int)data.length, digest );
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
}

@end
